<?php
if(!isset($_REQUEST['todo']) || !isset($_REQUEST['cid'])) die('Invalid usage - missing param');
$cid = $_REQUEST['cid'];
$todo = $_REQUEST['todo'];

if(isset($_REQUEST['counselor']))
	$counselor = $_REQUEST['counselor'];
else
	$counselor = null;

define('CID', $cid);
define('READ_ONLY_SESSION',true);
require_once('../../../include.php');
ModuleManager::load_modules();

$rs = Utils_RecordBrowserCommon::get_records('SCTV_StatusTodo', array('todo'=>$todo));
$statuses = array();
foreach($rs as $key=>$value)
{
	$statuses[]=$value['status'];
}

$crits = array('status'=>$statuses);
if($counselor != null)
{
	$crits['counselor'] = $counselor;
}

$recs = Utils_RecordBrowserCommon::get_records('SCTV_Requirement', $crits);

set_time_limit(0);		
		
$cols = array(
	array('name'=>__('Status'), 'width'=>30),
	array('name'=>__('Customer Name'), 'width'=>30),
	array('name'=>__('Address'), 'width'=>10),
	array('name'=>__('Street'), 'width'=>30),
	array('name'=>__('Area'), 'width'=>15),				
	array('name'=>__('Phone'), 'width'=>15),
	array('name'=>__('Cable Status'), 'width'=>10),
	array('name'=>__('Number Of HD'), 'width'=>10),
	array('name'=>__('Number Of SD'), 'width'=>10),
	array('name'=>__('Internet Package'), 'width'=>10),
	array('name'=>__('Bundle'), 'width'=>10),
	array('name'=>__('Counselor'), 'width'=>25),				
	array('name'=>__('Input Date'), 'width'=>15),
	array('name'=>__('Requirement Code'), 'width'=>10),
	array('name'=>__('Note'), 'width'=>30)
);
		
require_once 'modules/Libs/PHPExcel/lib/PHPExcel.php';
require_once 'modules/Libs/PHPExcel/lib/PHPExcel/Writer/Excel5.php';
		
$objPHPExcel = new PHPExcel();
		
$column = 0;$row = 1;
$sheet = $objPHPExcel->getActiveSheet();
		
foreach ($cols as $key=>$value){
	$sheet->getColumnDimensionByColumn($column)->setWidth($value['width']);		
	$sheet->setCellValueByColumnAndRow($column, $row, $value['name']);
	$column++;
}			

foreach ($recs as $value) {
	$rec = array();
	
	$val = Utils_CommonDataCommon::get_value('SCTV/Requirement/Status/'.$value['status'], true);
	$rec[] = SCTV_ReportsCommon::processing($val);
	$val = $value['customer_name'];
	$rec[] = SCTV_ReportsCommon::processing($val);
	$val = $value['address'];
	$rec[] = SCTV_ReportsCommon::processing($val);
	$street_area = Utils_RecordBrowserCommon::get_val('SCTV_Requirement', 'street', $value);
	$pos1 = stripos($street_area, '[');
	$pos2 = strripos($street_area, ']');
	$street = substr($street_area, 0, $pos1);
	$area = substr($street_area, $pos1+1, $pos2-$pos1-1);	
	$rec[] = SCTV_ReportsCommon::processing($street);	
	$rec[] = SCTV_ReportsCommon::processing($area);					
	$val = $value['phone'];
	$rec[] = SCTV_ReportsCommon::processing($val);
	$val = SCTV_RequirementCommon::get_cable_status_name($value['cable_status']);
	$rec[] = SCTV_ReportsCommon::processing($val);
	$val = $value['number_of_hd'];
	$rec[] = SCTV_ReportsCommon::processing($val);
	$val = $value['number_of_sd'];
	$rec[] = SCTV_ReportsCommon::processing($val);
	$val = Utils_RecordBrowserCommon::get_val('SCTV_Requirement', 'internet_package', $value);
	$rec[] = SCTV_ReportsCommon::processing($val);
	$val = Utils_RecordBrowserCommon::get_val('SCTV_Requirement', 'bundle', $value);
	$rec[] = SCTV_ReportsCommon::processing($val);
	$val =  Utils_RecordBrowserCommon::get_val('SCTV_Requirement', 'counselor', $value);
	$rec[] = SCTV_ReportsCommon::processing($val);
	$val = date('d-m-Y', strtotime($value['input_date']));			
	$rec[] = SCTV_ReportsCommon::processing($val);
	$val = $value['requirement_code'];
	$rec[] = SCTV_ReportsCommon::processing($val);
	$val = $value['note'];

	$row++;$column = 0;
		
	foreach ($rec as $value){			
		$sheet->setCellValueByColumnAndRow($column, $row, $value);			
		$column++;
	}
}
	
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
		
header('Content-Encoding: UTF-8');
header('Content-type: text/xls; charset=UTF-8');
header('Content-disposition: attachement; filename="'. $report .date('Y_m_d__H_i_s').'.xls"');
$objWriter->save('php://output');

?>
